﻿using System;
using System.Collections;
using System.Configuration;
using System.Data;
using System.Linq;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;
using System.Xml.Linq;
using eschoolBO;

namespace ESCHOOL.admin
{
    public partial class SchoolInfo : System.Web.UI.Page
    {
        branch b = new branch();
        protected void Page_Load(object sender, EventArgs e)
        {
            btnupdate.Visible = false;
            if (!IsPostBack)
            {
                DataTable dt = b.GetRecords(-99, "", "", "", "", false, " and ");
                GridView1.DataSource = dt;
                GridView1.DataBind();
            }
        }

        protected void btnedit_Command(object sender, CommandEventArgs e)
        {
            btninsert.Visible = false;
            btnupdate.Visible = true;
            DataTable dt=b.GetRecords(Convert.ToInt64(e.CommandArgument),"","","","",false," and ");
            tbbranchid.Text = Convert.ToString(dt.Rows[0]["branch_id"].ToString());
            tbaddress.Text = dt.Rows[0]["address"].ToString();
            tbdecription.Text = dt.Rows[0]["description"].ToString();
            TextBox1.Text = dt.Rows[0]["school_image"].ToString();
            tbmanager.Text = dt.Rows[0]["branch_manager"].ToString();
            
            
        }

        protected void btndelete_Command(object sender, CommandEventArgs e)
        {
            if (e.CommandName == "Delete")
            {
                DataTable dt = b.GetRecords(Convert.ToInt64(e.CommandArgument),"","","","",false," and ");
                b.branch_id=Convert.ToInt64(dt.Rows[0]["branch_id"].ToString());
                b.address = dt.Rows[0]["address"].ToString();
                b.description = dt.Rows[0]["description"].ToString();
                b.school_image = dt.Rows[0]["school_image"].ToString();
                b.branch_manager = dt.Rows[0]["branch_manager"].ToString();
                b.isdeleted = true;
                b.SaveRecords();
                Response.Redirect("~/admin/SchoolInfo.aspx");
            }
        }

        protected void btninsert_Click(object sender, EventArgs e)
        {
            b.branch_id = -99;
            b.address = tbaddress.Text;
            b.description = tbdecription.Text;
            if (FileUpload1.HasFile)
                FileUpload1.PostedFile.SaveAs(Server.MapPath("~/admin/school/" + FileUpload1.FileName));
            b.school_image = FileUpload1.FileName;
            b.branch_manager = tbmanager.Text;
            b.isdeleted = false;
            b.SaveRecords();
            Response.Redirect("~/admin/SchoolInfo.aspx");

        }

        protected void GridView1_RowEditing(object sender, GridViewEditEventArgs e)
        {

        }

        protected void btnupdate_Click(object sender, EventArgs e)
        {
            b.branch_id = Convert.ToInt64(tbbranchid.Text);
            b.address = tbaddress.Text;
            b.description = tbdecription.Text;
            if(FileUpload1.HasFile)
            {
                FileUpload1.PostedFile.SaveAs(Server.MapPath("~/admin/school/"+FileUpload1.FileName));
                b.school_image = FileUpload1.FileName;
            }
            else
                b.school_image = TextBox1.Text;
            b.branch_manager = tbmanager.Text;
            b.isdeleted = false;
            b.SaveRecords();
            Response.Redirect("~/admin/SchoolInfo.aspx");
        }

        protected void GridView1_SelectedIndexChanged(object sender, EventArgs e)
        {

        }

        protected void GridView1_PageIndexChanging(object sender, GridViewPageEventArgs e)
        {

        }
    }
}
